package cn.edu.nbpt.campus_api.service.campus.impl;

import cn.edu.nbpt.campus_api.exception.BizException;
import cn.edu.nbpt.campus_api.mapper.campus.ClassMapper;
import cn.edu.nbpt.campus_api.mapper.campus.StudentMapper;
import cn.edu.nbpt.campus_api.model.ErrorCode;
import cn.edu.nbpt.campus_api.model.Page;
import cn.edu.nbpt.campus_api.model.campus.entity.Clazz;
import cn.edu.nbpt.campus_api.model.campus.entity.Student;
import cn.edu.nbpt.campus_api.model.campus.vo.ClassStudentVo;
import cn.edu.nbpt.campus_api.service.campus.ClassService;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.*;

@Service
public class ClassServiceImpl implements ClassService {

    @Resource
    private ClassMapper classMapper;

    @Resource
    private StudentMapper studentMapper;

    @Override
    public Page<Clazz> selectClassList(String name, String code, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Clazz> classes = classMapper.selectClassList(name, code);
        return Page.page(classes);
    }

    @Override
    public Clazz selectClass(Integer id) {
        Clazz clazz = classMapper.selectClass(id);
        if (clazz == null) throw new BizException(ErrorCode.SELECT_ERROR);
        return clazz;
    }

    @Transactional
    @Override
    public Integer insertClass(ClassStudentVo classStudentVo) {
        classStudentVo.setCreateBy("admin");
        classStudentVo.setCreateTime(new Date());
        Integer row = classMapper.insertClass(classStudentVo);
        if (row == 0) throw new BizException(ErrorCode.INSERT_ERROR);
        insertStudentList(classStudentVo);
        return classStudentVo.getId();
    }

    @Transactional
    @Override
    public Integer updateClass(ClassStudentVo classStudentVo) {
        classStudentVo.setCreateBy("user");
        classStudentVo.setCreateTime(new Date());
        Integer row = classMapper.updateClass(classStudentVo);
        if (row == 0) throw new BizException(ErrorCode.UPDATE_ERROR);
        studentMapper.deleteStudentInClass(Collections.singletonList(classStudentVo.getId()));
        insertStudentList(classStudentVo);
        return classStudentVo.getId();
    }

    @Transactional
    @Override
    public Integer deleteClass(List<Integer> ids) {
        Integer row = classMapper.deleteClass(ids);
        if (row == 0) throw new BizException(ErrorCode.DELETE_ERROR);
        studentMapper.deleteStudentInClass(ids);
        return null;
    }

    private void insertStudentList(ClassStudentVo classStudentVo) {
        List<Student> studentList = classStudentVo.getStudents();
        Integer id = classStudentVo.getId();
        if (!CollectionUtils.isEmpty(studentList)) {
            List<Student> list = new ArrayList<Student>();
            for (Student student : studentList) {
                student.setClassId(id);
                list.add(student);
            }
            if (list.size() > 0) {
                studentMapper.addStudentList(list);
            }
        }
    }
}
